﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using SharpMap.Layers;
using SharpMap.Data.Providers;
using SharpMap.Control;

namespace AHP2
{
    public partial class FrmMain : Form
    {
        private MapPane _paneMap = null;
        private MapTreePane _paneMapTree = null;
        private SettingsPane _paneSetting = null;

        private AxSharpMapControl _mapControl = null;
        private TreeView _mapTree = null;
        

        public FrmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitPanes();
            InitMapControl();
        }

        #region Menu Map Operation
        private void OnMenuNewMap_Click(object sender, EventArgs e)
        {

        }

        private void OnMenuCloseMap_Click(object sender, EventArgs e)
        {

        }

        private void OnMenuAddLayer_Click(object sender, EventArgs e)
        {
            AddLayer();
        }

        private void OnMenuExit_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region Menu MapView
        private void OnMenuZoomIn_Click(object sender, EventArgs e)
        {
            this._mapControl.ZoomIn();
        }

        private void OnMenuZoomOut_Click(object sender, EventArgs e)
        {
            this._mapControl.ZoomOut();
        }

        private void OnMenuPan_Click(object sender, EventArgs e)
        {
            this._mapControl.Pan();
        }

        private void OnMenuFullScreen_Click(object sender, EventArgs e)
        {
            this._mapControl.ZoomToExtents();            
            this._mapControl.RefreshMap();
        }

        private void OnMenuMapRefresh_Click(object sender, EventArgs e)
        {
            this._mapControl.RefreshMap();
        }
        #endregion
        

        #region Toolbar MapView
        private void OnMapZoomIn_Click(object sender, EventArgs e)
        {
            this._mapControl.ZoomIn();
        }

        private void OnMapZoomOut_Click(object sender, EventArgs e)
        {
            this._mapControl.ZoomOut();
        }

        private void OnMapPan_Click(object sender, EventArgs e)
        {
            this._mapControl.Pan();
        }

        private void OnMapFullScreen_Click(object sender, EventArgs e)
        {
            this._mapControl.ZoomToExtents();
            this._mapControl.RefreshMap();
        }

        private void OnMapRefresh_Click(object sender, EventArgs e)
        {
            this._mapControl.RefreshMap();
        }
        #endregion

        #region Toolbar Map Operation
        private void OnAddLayer_Click(object sender, EventArgs e)
        {
            AddLayer();
        }
        #endregion

        #region Form Settings
        private void InitPanes()
        {
            _paneMapTree = new MapTreePane();
            _paneMapTree.Show(this.dockPanel1, DockState.DockLeft);

            _paneSetting = new SettingsPane();
            _paneSetting.Show(this.dockPanel1, DockState.DockRightAutoHide);

            _paneMap = new MapPane();
            _paneMap.Show(this.dockPanel1);

            _mapControl = _paneMap.MapControl;
            _mapTree = _paneMapTree.MapTree;
        }
        #endregion

        #region MapOperatoin
        /// <summary>
        /// 添加图层到SharpMapControl
        /// </summary>
        private void AddLayer()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "shape files(*.shp)|*.shp" + 
                         "|tif(*.tif)|*.tif" +
                         "|jpg(*.jpg)|*.jpg" +
                         "|png(*.png)|*.png";
            if (dlg.ShowDialog() == DialogResult.OK)
            {   
                string strFileName = dlg.FileName;
                Layer layer = OpenLayer(strFileName);
                if (layer != null)
                {
                    this._mapControl.AddLayer(layer);
                    RefreshMap();
                }
            }
        }

        /// <summary>
        /// 打开文件图层
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        private Layer OpenLayer(string strPath)
        {
            Layer layer = null;
            string layer_name = System.IO.Path.GetFileNameWithoutExtension(strPath);
            if (strPath.EndsWith(".shp"))
            {
                VectorLayer vlayer = new VectorLayer(layer_name);
                vlayer.DataSource = new ShapeFile(strPath);
                layer = vlayer;
            }
            else
            {
                layer = new GdalRasterLayer(layer_name, strPath);
            }
            return layer;
        }

        private void RefreshMap()
        {
            SharpMap.Map map = this._mapControl.Map;
            if (map.Layers.Count ==0)
            {
                return;
            }
            else if (map.Layers.Count == 1)
            {
                //this._mapControl.ZoomToExtents();
                this._mapControl.FullScreen();
            }
            
            this._mapControl.RefreshMap();
        }
        #endregion

        #region MapControl
        private void InitMapControl()
        {   
            _paneMap.CoordinateLabel = this.statusLabel_coordinate;
        }
        #endregion

    }
}
